//
//  TreeNovel.swift
//  AQDeal
//
//  Created by zhangjikuan on 2020/5/28.
//  Copyright © 2020 hsgd. All rights reserved.
//

import Foundation

struct TreeNovel {
    
    public func readTxt() {
        
        let path:URL? =
            Bundle.main.url(forResource: "yang", withExtension: "txt")
        
        print(path ?? "read error")
        
        let data = try! Data(contentsOf: path!)
        
        print(data);
        let content = try! String.init(contentsOf: path!)
        
        //        print(content)
        
        // 正则
        let parten = "第[0-9一二三四五六七八九十百千]*[章回].*"
        
        // 搜索
        var results:[NSTextCheckingResult] = []
        
        do{
            let regularExpression:NSRegularExpression = try NSRegularExpression(pattern: parten, options: .caseInsensitive)
            
            results = regularExpression.matches(in: content, options: .reportCompletion, range: NSRange(location: 0, length: content.count))
            
        }catch{
            
        }
        if !results.isEmpty {
            
            // 记录最后一个Range
            var lastRange = NSMakeRange(0, 0)
            
            // 数量
            let count = results.count
            
            // 记录 上一章 模型
            
            
            for i in 0...count {
                
                // 章节数量分析:
                // count + 1  = 搜索到的章节数量 + 最后一个章节,
                // 1 + count + 1  = 第一章前面的前言内容 + 搜索到的章节数量 + 最后一个章节
                print("总章节数:\(count + 1)  当前解析到:\(i + 1)")
                // range
                var range = NSMakeRange(0, 0)
                
                var location = 0
                
                if i < count {
                    
                    range = results[i].range
                    
                    location = range.location
                }
                
                let content =  ContentTypesetting(content: content.substring(NSMakeRange(lastRange.location, location - lastRange.location)))
                
                print(content)
                // 记录
                lastRange = range
                
            }
        }
    }
    public func ContentTypesetting(content:String) ->String {
        
        // 替换单换行
        var content = content.replacingOccurrences(of: "\r", with: "")
        
        // 替换换行 以及 多个换行 为 换行加空格
        content = content.replacing(pattern: "\\s*\\n+\\s*", template: "\n　　")
        
        // 返回
        return content
    }
    
    public func sections() -> [Section] {
        return []
    }
    
    public func catalogue() -> Catalogue {
        
        return Catalogue.init()
    }
    
    public func chapter(_ section:Section) -> Chapter {
        
        return Chapter.init(section: section, title: "111", content: "sss")
    };
}

/// 分级 具体类
struct Catalogue {
    var sections = [Section]()
    var name:String?
}

struct Section {
    var id:String?
    
    var chapters = [Chapter]()
    var catalogue:Catalogue?
    var sectionName:String?
}

struct Chapter {
    var section: Section?
    var title: String?
    var content:String?
}

/// 以一种语法树的形式编译文件


///
